/**
 * 
 */
package sopia.sg;

import sopia.cmm.CommonFunct;
import sopia.conn.AccountAdapter;
import sopia.conn.CategoryAdapter;
import sopia.conn.DBAdapter;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.KeyEvent;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageButton;
import android.widget.Spinner;
import android.widget.Toast;

/**
 * @author tatsu@gmail.com
 *
 */
public class G007Activity extends Activity  {
	private CategoryAdapter categoryAdapter ;
	private Button addCategory_button ;
	private EditText nameCategory_edit ;
	private String screenID ;
	/**
	 * 
	 */
	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.g007_layout);
		//init spinner
//		Spinner typeAccSpinner = (Spinner) findViewById(R.id.unit_spin);
//		ArrayAdapter adapter = ArrayAdapter.createFromResource(
//		            this, R.array.unit, android.R.layout.simple_spinner_item);
//	    adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
//	    typeAccSpinner.setAdapter(adapter);
		 // screen ID call
		Intent receiveIntent = this.getIntent();
		this.screenID = receiveIntent.getStringExtra("ID");
		// add account addCategory_button 
		addCategory_button = (Button) findViewById(R.id.addCategory_button);
		nameCategory_edit = (EditText) findViewById(R.id.nameCategory_edit);
		addCategory_button.setOnClickListener(new OnClickListener() {
			
			@Override
			public void onClick(View v) {
				String name = nameCategory_edit.getText().toString();
				insertCategory(name);
				
				
			}
		});
	}
	@Override
	public boolean onKeyDown(int keyCode, KeyEvent event) {
	  //Handle the back button
	  if (keyCode == KeyEvent.KEYCODE_BACK) {
		  backToPreviousScreen();
	    return true;
	  }
	  return super.onKeyDown(keyCode, event);
	} 
	private void insertCategory(String name){
		try {
			categoryAdapter = new CategoryAdapter(this);	
			categoryAdapter.beginTransaction();
			if(CommonFunct.isNullOrEmpty(name)){
				name = null;
				Toast.makeText(this,"Please input [Category name]!" , Toast.LENGTH_LONG).show();
				nameCategory_edit.setFocusable(true);
				return;
			}
			Long id = categoryAdapter.insertCategory(name.trim());
			//the row ID of the newly inserted row, or -1 if an error occurred 
			String contentMessage = "Category ["+name;
			if(id < 0){			
				contentMessage += "] really exist your database.";
				Toast.makeText(this,contentMessage , Toast.LENGTH_LONG).show();
				nameCategory_edit.selectAll();
			}else{
				contentMessage += "] was added.";
				Toast.makeText(this, contentMessage , Toast.LENGTH_LONG).show();
				backToPreviousScreen();
			}
			categoryAdapter.setTransactionSuccessful();
		} catch (Exception e) {
			Log.e("Error in transaction", e.toString());
		}finally {
			categoryAdapter.endTransaction();
			categoryAdapter.close();
		}
	}
	private void backToPreviousScreen() {
		Intent i = null;
		if("G004".equals(this.screenID)){
			i = new Intent(G007Activity.this, G004Activity.class);
		}
		i.putExtra("ID", "G007");
//		Bundle sendBundle = new Bundle();
//		sendBundle.putLong("value", receiveValue);
//		i.putExtras(sendBundle);
		startActivity(i);
		finish();
	}
	

}
